package com.quectel.cms.controller.alarm;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.alarm.dto.AlarmRuleConditionDto;
import com.quectel.core.module.alarm.service.AlarmRuleConditionService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-05-31 12:23:34
 */
@RestController
@RequestMapping("alarmRuleCondition")
@Tag(name = "AlarmRuleConditionController", description = "告警规则条件相关api")
public class AlarmRuleConditionController extends BaseController {

    @DubboReference
    private AlarmRuleConditionService alarmRuleConditionService;

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "alarmCondition:list")
    @Operation(summary = "告警规则条件的查询列表")
    public Response<Page<AlarmRuleConditionDto>> list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Request request = Request.configParams(params);

        List<AlarmRuleConditionDto> list = alarmRuleConditionService.queryList(params);
        int total = alarmRuleConditionService.queryTotal(params);

        Page<AlarmRuleConditionDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<AlarmRuleConditionDto>>ok().wrap(page);
    }


    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "alarmCondition:info")
    @Operation(summary = "查询单条告警规则条件记录")
    public Response<AlarmRuleConditionDto> info(@PathVariable("id") Long id) {

        AlarmRuleConditionDto alarmRuleConditionDto = alarmRuleConditionService.selectById(id);

        return Response.<AlarmRuleConditionDto>ok().wrap(alarmRuleConditionDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "alarmCondition:edit")
    @Operation(summary = "保存单条告警规则条件记录")
    public Response<Object> save(@RequestBody @Validated AlarmRuleConditionDto alarmRuleConditionDto) {

        //填充基础数据
        fillSaveCommonData(alarmRuleConditionDto);

        alarmRuleConditionService.save(alarmRuleConditionDto);

        return Response.ok();
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "alarmCondition:edit")
    @Operation(summary = "修改单条告警规则条件记录")
    public Response<Object> update(@RequestBody @Validated AlarmRuleConditionDto alarmRuleConditionDto) {

        //填充基础数据
        fillEditCommonData(alarmRuleConditionDto);

        alarmRuleConditionService.updateById(alarmRuleConditionDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "alarmCondition:delete")
    @Operation(summary = "批量删除存在的告警规则条件记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        alarmRuleConditionService.deleteBatch(ids);

        return Response.ok();
    }

    /**
     * 根据告警规则Id查询
     */
    @GetMapping("listByRuleId/{alarmRuleId}")
    @Operation(summary = "通过条件查询告警条件列表")
    public Response<List<AlarmRuleConditionDto>> listByRuleId(@PathVariable("alarmRuleId") Long alarmRuleId) {

        List<AlarmRuleConditionDto> alarmRuleConditionDtoList = alarmRuleConditionService.selectByRuleId(alarmRuleId);

        return Response.<List<AlarmRuleConditionDto>>ok().wrap(alarmRuleConditionDtoList);
    }
}
